package service.script;


import java.util.ArrayList;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import scriptrunner.execution.FunctionRegister;
import scriptrunner.execution.cloud.CloudManager;
import service.AbstractService;
import service.script.builtin.GetLogRequestHandler;
import service.script.builtin.WriteLogRequestHandler;

public class ScriptExecutionService extends AbstractService {
	
	private ArrayList<String> logs = new ArrayList<String>();

	public ScriptExecutionService() {
		BasicConfigurator.configure();
		Logger.getLogger("org.jgroups").setLevel(Level.ERROR);
	}
	
	@Override
	public void init() throws Exception {
		this.coWorkers.addRequestHandler(new ScriptExecutionRequestHandler(this));
		this.coWorkers.addRequestHandler(new FunctionExecuteRequestHandler(this));
		this.coWorkers.addRequestHandler(new WriteLogRequestHandler(this));
		this.coWorkers.addRequestHandler(new GetLogRequestHandler(this));
		CloudManager.createCloudManager("GreenCloud", coWorkers);
		
		String[] preFunc = {"scriptrunner.execution.WriteLogFunction",
				"scriptrunner.execution.ServerTimeFunction",
				"scriptrunner.execution.ListAddElementFunction"};
		
		FunctionRegister.GetInstance().RegisterPredefinedFunction(preFunc);
	}
	
	public void writeLog(String log) {
		logs.add(log);
	}
	
	public ArrayList<String> getLog() {
		return logs;
	}

}
